package com.xmut.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmut.eduservice.client.VodClient;
import com.xmut.eduservice.entity.EduVideo;
import com.xmut.eduservice.mapper.EduVideoMapper;
import com.xmut.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-04
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {

        // 根据课程id查询所有视频id
        QueryWrapper<EduVideo> wrapperVideoSourceId = new QueryWrapper<>();
        wrapperVideoSourceId.eq("course_id", courseId);
        wrapperVideoSourceId.select("video_source_id");
        List<EduVideo> list = this.list(wrapperVideoSourceId);

        List<String> videoIds = new ArrayList<>();
        for(int i = 0; i < list.size(); ++i) {
            EduVideo eduVideoTemp = list.get(i);
            String videoSourceId = eduVideoTemp.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {
                videoIds.add(videoSourceId);
            }
        }
        if (videoIds.size() > 0) {
            vodClient.removeVideoList(videoIds);
        }


        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        this.remove(queryWrapper);
    }
}
